package com.shuqi.common.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.cc.cache.ImageManager;
import com.cc.cache.core.ImageCacheOption;
import com.cc.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.cc.cache.memory.MemoryCacheAware;
import com.cc.cache.memory.impl.LRULimitedMemoryCache;
import com.cc.cache.utils.StorageUtils;
import com.shuqi.controller.R;

public class SqImageManager {
    /** 默认的图片缓存类型，该类型会在内存和磁盘中同时缓存图片 */
    public static final String IMG_DEFAULT = "default";
    /** 封面对应的图片缓存类型，该类型会在内存和磁盘中同时缓存图片 */
    public static final String IMG_COVER = "cover";
    /** 大图对应的图片缓存类型，该类型只会在磁盘中缓存图片 */
    public static final String IMG_BIG_PICTURE = "bigPictrue";

    private static ImageManager sInnerManager;

    private SqImageManager() {

    }

    public static ImageManager getImageManager() {
        return sInnerManager;
    }

    /**
     * 用来初始化缓存系统。已在Application的Oncreate中调用此方法，使用此工具类的同学请不要再次调用！
     * 
     * @param context
     */
    public static synchronized void init(Context context) {
        if (sInnerManager != null) {
            throw new IllegalStateException("重复初始化！");
        }

        sInnerManager = ImageManager.getInstance();
        MemoryCacheAware<String, Bitmap> memoryCache =
                new LRULimitedMemoryCache<Bitmap>(6 * 1024 * 1024);
        sInnerManager.addImageCacheOption(new ImageCacheOption.Builder(IMG_DEFAULT)
                .showStubImage(R.drawable.icon_def_bookimg_list)
                .cacheInMemory()
                .cacheOnDisc()
                .memoryCache(memoryCache)
                .discCache(
                        new TotalSizeLimitedDiscCache(StorageUtils.getOwnCacheDirectory(context,
                                IMG_DEFAULT), 50 * 1024 * 1024))
                .bitmapConfig(Bitmap.Config.RGB_565).build());
        sInnerManager.addImageCacheOption(new ImageCacheOption.Builder(IMG_COVER)
                .showStubImage(R.drawable.icon_def_bookimg_list)
                .cacheInMemory()
                .cacheOnDisc()
                .memoryCache(memoryCache)
                .discCache(
                        new TotalSizeLimitedDiscCache(StorageUtils.getOwnCacheDirectory(context,
                                IMG_COVER), 50 * 1024 * 1024)).bitmapConfig(Bitmap.Config.RGB_565)
                .build());
        sInnerManager.addImageCacheOption(new ImageCacheOption.Builder(IMG_BIG_PICTURE)
                .showStubImage(R.drawable.icon_def_bookimg_list)
                .cacheOnDisc()
                .discCache(
                        new TotalSizeLimitedDiscCache(StorageUtils.getOwnCacheDirectory(context,
                                IMG_BIG_PICTURE), 100 * 1024 * 1024))
                .bitmapConfig(Bitmap.Config.RGB_565).build());
    }

}
